﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Projektet.View
{
    class GameView
    {
        Model.GameModel _gameModel;

        SpriteFont _spriteFont;

        Texture2D _pikemanTexture;
        Texture2D _knightTexture;
        Texture2D _archerTexture;
        Texture2D _castleTexture;
        Texture2D _npcCastleTexture;
        Texture2D _backgroundTexture;

        public GameView(Model.GameModel gameModel) 
        {
            _gameModel = gameModel;
            //View.Camera.Resolution;
        }

        public void LoadContent(ContentManager contManager)
        {
            _pikemanTexture = contManager.Load<Texture2D>("Pikeman");
            _knightTexture = contManager.Load<Texture2D>("testKnight");
            _archerTexture = contManager.Load<Texture2D>("Archer");
            _castleTexture = contManager.Load<Texture2D>("testCastle");
            _npcCastleTexture = contManager.Load<Texture2D>("testCastleNpc");
            _backgroundTexture = contManager.Load<Texture2D>("background");
            _spriteFont = contManager.Load<SpriteFont>("SpriteFont1");
        }

        public void Draw(SpriteBatch spriteBatch) 
        {
            DrawBackground(spriteBatch);
            DrawUi(spriteBatch);
            foreach(Model.CombatUnit cUnit in _gameModel._humanPlayer._unitList)
            {
                switch(cUnit.GetType().Name)
                {
                    case "Pikeman":
                        DrawPikeman(spriteBatch, cUnit);
                        break;
                    case "Knight":
                        DrawKnight(spriteBatch, cUnit);
                        break;
                    case "Archer":
                        DrawArcher(spriteBatch, cUnit);
                        break;
                    case "Castle":
                        DrawCastle(spriteBatch, cUnit);
                        break;
                }
            }

            foreach (Model.CombatUnit cUnit in _gameModel._npcPlayer._unitList)
            {
                switch (cUnit.GetType().Name)
                {
                    case "Pikeman":
                        DrawPikeman(spriteBatch, cUnit, true);
                        break;
                    case "Knight":
                        DrawKnight(spriteBatch, cUnit, true);
                        break;
                    case "Archer":
                        DrawArcher(spriteBatch, cUnit, true);
                        break;
                    case "Castle":
                        DrawCastle(spriteBatch, cUnit, true);
                        break;
                }
            }            
        }

        public void DrawUi(SpriteBatch spriteBatch) 
        {
            string gold = "Gold: " + ((int)_gameModel._humanPlayer._gold).ToString();
            Vector2 position = new Vector2(10, 20);
            spriteBatch.DrawString(_spriteFont, gold, position, Color.White);
        }

        public void DrawPikeman(SpriteBatch spriteBatch, Model.CombatUnit cUnit, bool flip = false) 
        {
            Rectangle visualRect = Camera.GetVisualRectangle(cUnit._unitRect);
            if(cUnit._playerType == Model.PlayerType.Human)
            {
                spriteBatch.Draw(_pikemanTexture, visualRect, Color.White);
            }
            else
            {
                spriteBatch.Draw(_pikemanTexture, visualRect, null, Color.White, 0f, new Vector2(), SpriteEffects.FlipHorizontally, 0.9f);
            }
        }

        public void DrawArcher(SpriteBatch spriteBatch, Model.CombatUnit cUnit, bool flip = false) 
        {
            Rectangle visualRect = Camera.GetVisualRectangle(cUnit._unitRect);
            if (cUnit._playerType == Model.PlayerType.Human)
            {
                spriteBatch.Draw(_archerTexture, visualRect, Color.White);
            }
            else
            {
                spriteBatch.Draw(_archerTexture, visualRect, null, Color.White, 0f, new Vector2(), SpriteEffects.FlipHorizontally, 0.9f);
            }
        }

        public void DrawKnight(SpriteBatch spriteBatch, Model.CombatUnit cUnit, bool flip = false) 
        {
            Rectangle visualRect = Camera.GetVisualRectangle(cUnit._unitRect);
            if (cUnit._playerType == Model.PlayerType.Human)
            {
                spriteBatch.Draw(_knightTexture, visualRect, Color.White);   
            }
            else
            {
                spriteBatch.Draw(_knightTexture, visualRect, null, Color.White, 0f, new Vector2(), SpriteEffects.FlipHorizontally, 0.9f);
            }
        }

        public void DrawCastle(SpriteBatch spriteBatch, Model.CombatUnit cUnit, bool flip = false) 
        {
            Rectangle visualRect = Camera.GetVisualRectangle(cUnit._unitRect);
            if(cUnit._playerType == Model.PlayerType.Human)
            {
                spriteBatch.Draw(_castleTexture, visualRect, Color.White);
            }
            else
            {
                spriteBatch.Draw(_npcCastleTexture, visualRect, Color.White);
            }            
        }

        public void DrawBackground(SpriteBatch spriteBatch) 
        {
            Rectangle backgroundRect = new Rectangle(-20 - Camera.GetVisualOffset(), 0, (int)Camera.Resolution.X * 2, (int)Camera.Resolution.Y);
            spriteBatch.Draw(_backgroundTexture, backgroundRect, Color.White);
        }
    }
}
